
const xlsx = require('node-xlsx')//引入模块
const fs = require('fs')
const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

let mix = 0,max = 0, count = 0
let decimal = 0
function getMix() {
    readline.question(`请输入最小成绩? `, name => {
        mix = name
        if (checkNum(mix)) {
            mix = mix.toString()
            if (mix.indexOf(".") != -1) {
                decimal = mix.length - mix.indexOf(".") -1
            }
            getMax()
        } else {
            getMix()
        }        
    })
} 

function getMax() {
    readline.question(`请输入最大成绩? `, name => {
        max = name
        if (checkNum(max) && Number(max) > Number(mix)) {
            max = max.toString()
            if (max.indexOf(".") != -1) {
                let len = max.length - max.indexOf(".") -1
                decimal = decimal > len ? decimal : len
            }
            getCount()
        } else {
            getMax()
        }
    })
}

function getCount() {
    readline.question(`请输入人数? `, name => {
        count = name
        if (checkNum(count) && Number(count) > 0) {
            getScore()
        } else {
            getCount()
        }
    })
}

function checkNum(num){
    return Number.isFinite(Number(num))    
}

function getScore() {
    readline.close()
    mix = Number(mix)
    max = Number(max)
    count = Number(count)
    let scoreList = []
    for (let index = 0; index < count; index++) {
        let score = Math.random()*(max-mix +1)+mix
        score = formatNumToFixed(score,decimal)
        scoreList.push(score)
        console.log(score)
    }
    genXls(scoreList)
}

function formatNumToFixed(num, idx) {
    if (num > max) {
        num = max
    }  
    if (num < mix) {
        num = mix
    }
    return Number(num.toFixed(idx));
}

function genXls(scoreList) {
    let data = [] // 其实最后就是把这个数组写入excel   
    let title = ['成绩']//这是第一行 俗称列名 
    data.push(title) // 添加完列名 下面就是添加真正的内容了
    for (const score of scoreList) {
        let arrInner = []
        arrInner.push(score)
        data.push(arrInner)//data中添加的要是数组
    }
    writeXls(data)
}

function  writeXls(datas) {
    let buffer = xlsx.build([
        {
            name:'sheet1',
            data:datas
        }
    ]);
    fs.writeFileSync('./score.xlsx',buffer,{'flag':'w'});//生成excel the_content是excel的名字，大家可以随意命名
}

getMix()